package com.meilai.project.controller.web.business.contract;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.controller.web.business.contract.dto.ContractApplySaveReq;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.business.contract.ContractApplyCheckDTO;
import com.meilai.project.dto.business.contract.ContractApplyLatestListDTO;
import com.meilai.project.dto.business.contract.ContractApplyListDTO;
import com.meilai.project.entity.business.contract.ContractApply;
import com.meilai.project.service.business.contract.BillNumberService;
import com.meilai.project.service.business.contract.ContractApplyService;
import com.meilai.project.service.business.intentionScheme.IntentionSchemeService;
import com.meilai.project.vo.business.contract.ContractApplyForADMakeVO;
import com.meilai.project.vo.business.contract.ContractApplyVO;
import com.meilai.project.vo.business.contract.NotCompleteSaleContractVO;
import com.meilai.project.vo.business.intentionScheme.IntentionSchemeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xchen
 * @since 2022-03-02
 */
@Api(value = "合同申请controller", tags = "合同申请")
@RequestMapping("api/web/contract_apply")
@RestController
@Validated
public class ContractApplyController {

    @Autowired
    private ContractApplyService service;

    @Autowired
    private BillNumberService billNumberService;

    @Autowired
    private IntentionSchemeService intentionSchemeService;

	/*@PostMapping("generate")
	@ApiOperation(value = "多个方案生成合同")
	public ApiResult<Long> generateContract(@ApiParam("json") @RequestBody ContractGenerateDTO req) {
		return ApiResult.ok(service.generateContract(req));
	}*/

	/*@GetMapping("generate_contract_num")
	@ApiOperation(value = "生成合同编号", notes = "生成合同编号")
	public ApiResult<ContractAttachment> generate_attachment() {
		String contractNum = "XSHT" + DateUtil.format(new Date(), "yyyy");
		Integer currentNum = contractNumService.getCurrentNum(1);
		String numStr = StrUtil.fillBefore(currentNum.toString(), '0', 4);
		return ApiResult.ok(contractNum + numStr);
	}*/

    @GetMapping("list-not-related-intention-scheme-contract")
    @ApiOperation(value = "查询还未关联锁位的合同")
    public ApiResult<List<NotCompleteSaleContractVO>> listNotRelatedIntentionSchemeContract(
            @ApiParam("userId") @RequestParam(value = "userId", required = false) Long userId) {
        return ApiResult.ok(service.listNotComplete(userId));
    }

    @GetMapping("part_a_history")
    @ApiOperation(value = "甲方输入历史(根据客户ID查询)")
    public ApiResult<List<String>> partAHistory(@ApiParam("客户ID") @RequestParam Long customerId) {
        List<String> partAHistoryList = service.partAHistory(customerId);
        return ApiResult.ok(partAHistoryList);
    }

	/*@GetMapping("list-amend-contract")
	@ApiOperation(value = "查询补充协议合同")
	public ApiResult<AmendContractListVO> listByRootContractId(Long root_id) {
		List<ContractApplyVO> result = service.listByRootContractId(root_id);
		return ApiResult.ok(result);
	}*/

    @GetMapping("list-latest-by-customer-id")
    @ApiOperation(value = "根据customerId查合同列表")
    public ApiResult<List<ContractApplyVO>> listByCustomerId(@ApiParam("customerId") Long customerId) {
        List<ContractApplyVO> regularApplyVOPage = service.listLatestByCustomerId(customerId);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("list_by_customer_id")
    @ApiOperation(value = "根据customerId查合同列表，变更销售单时新增")
    public ApiResult<List<ContractApplyVO>> list_by_customer_id(@ApiParam("customerId") Long customerId) {
        List<ContractApplyVO> regularApplyVOPage = service.list_by_customer_id(customerId);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("list-latest-for-market-by-customer-id")
    @ApiOperation(value = "根据customerId查合同列表(for市场单)")
    public ApiResult<List<ContractApplyVO>> listForMarketByCustomerId(
            @ApiParam("customerId") Long customerId,
            @ApiParam("mustIncludeContractId") Long mustIncludeContractId
    ) {
        List<ContractApplyVO> regularApplyVOPage = service.listForMarketByCustomerId(customerId, mustIncludeContractId);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("list-latest-for-ad-make-by-dept-id")
    @ApiOperation(value = "根据deptId查合同列表(for制作单)")
    public ApiResult<List<ContractApplyForADMakeVO>> listForADMakeByDeptId(
            @ApiParam("customer_id") Long customer_id,
            @ApiParam("mustIncludeContractId") Long mustIncludeContractId
    ) {
        List<ContractApplyForADMakeVO> regularApplyVOPage = service.listForADMakeByDeptId(customer_id, mustIncludeContractId);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("list-by-root-id")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<ContractApplyVO>> listByRootId(Long rootContractId) {
        List<ContractApplyVO> regularApplyVOPage = service.listByRootContractId(rootContractId);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<ContractApplyVO>> list(@ApiParam("form") ContractApplyListDTO req) {
        Page<ContractApplyVO> regularApplyVOPage = service.selectPageList(req);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("list-latest")
    @ApiOperation(value = "查询最新合同-列表分页")
    public ApiResult<Page<ContractApplyVO>> listLatest(@ApiParam("form") ContractApplyLatestListDTO req) {
        Page<ContractApplyVO> regularApplyVOPage = service.selectLatestPageList(req);
        return ApiResult.ok(regularApplyVOPage);
    }

	/*@GetMapping("list-by-amend-type")
	@ApiOperation(value = "按类型查询补充协议列表分页")
	public ApiResult<Page<ContractApplyVO>> listByAmendType(@ApiParam("form") ContractApplyAmendListDTO req) {
		Page<ContractApplyVO> regularApplyVOPage = service.selectPageAmendList(req);
		return ApiResult.ok(regularApplyVOPage);
	}*/


    @GetMapping("detail/{id}")
    @ApiOperation(value = "单据明细")
    public ApiResult<ContractApplyVO> detail(@ApiParam("id") @PathVariable("id") Long id) {
        return ApiResult.ok(service.getOneById(id));
    }

    @GetMapping("info/{id}")
    @ApiOperation(value = "合同明细")
    public ApiResult<ContractApplyVO> info(@ApiParam("id") @PathVariable("id") Long id) {
        return ApiResult.ok(service.getOneById(id));
    }

    @GetMapping("get-by-intention-scheme-id")
    @ApiOperation(value = "单据明细(根据锁位id查)")
    public ApiResult<ContractApplyVO> getByIntentionSchemeId(@ApiParam("intentionSchemeId") Long intentionSchemeId) {
        IntentionSchemeVO schemeVO = intentionSchemeService.getOneById(intentionSchemeId);
        return ApiResult.ok(service.getOneById(schemeVO.getRelated_contract_id()));
    }

	/*@GetMapping("detail/date_range")
	@ApiOperation(value = "根据合同id和daterange_id查单据明细")
	public ApiResult<ContractApplyVO> detailDateRange(
			@ApiParam("id") @RequestParam("id") Long id,
			@ApiParam("id") @RequestParam("daterange_id") Long daterange_id
	) {
		return ApiResult.ok(service.getOneDateRangeById(id, daterange_id));
	}*/

    @PostMapping("save-attachment")
    @ApiOperation("保存附件")
    public ApiResult<Boolean> saveAttachment(@ApiParam("json") @RequestBody ContractApplySaveReq req) {
        try {
            Boolean apiResult = service.saveAttachment(req);
            return ApiResult.ok(apiResult);
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public ApiResult<Boolean> save(@ApiParam("json") @RequestBody ContractApplySaveReq req) {
        try {
            ApiResult<Boolean> apiResult = service.save(req);
            if (null == apiResult) {
                return ApiResult.ok("新增成功");
            }
            return apiResult;
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("begin")
    @ApiOperation("提交")
    public ApiResult<Boolean> begin(@ApiParam("json") @RequestBody ContractApplySaveReq req) {
        try {
            ApiResult<Boolean> apiResult = service.begin(req);
            if (null == apiResult) {
                return ApiResult.ok("提交成功");
            }
            return apiResult;
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("pass")
    @ApiOperation("通过")
    public ApiResult<Boolean> pass(@ApiParam("json") @RequestBody ContractApplyCheckDTO req) {
        try {
            if (service.pass(req)) {
                return ApiResult.ok("通过成功");
            } else {
                return ApiResult.err("通过失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("return_back")
    @ApiOperation("驳回")
    public ApiResult<Boolean> returnBack(@ApiParam("json") @RequestBody ContractApplyCheckDTO req) {
        try {
            if (service.returnBack(req)) {
                return ApiResult.ok("驳回成功");
            } else {
                return ApiResult.err("驳回失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "逻辑删除")
    public ApiResult<Boolean> delete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.delete(id)) {
                return ApiResult.ok("删除成功");
            } else {
                return ApiResult.err("删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("actual_delete")
    @ApiOperation(value = "物理删除")
    public ApiResult<Boolean> actualDelete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.actualDelete(id)) {
                return ApiResult.ok("彻底删除成功");
            } else {
                return ApiResult.err("彻底删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }


    @GetMapping("get_contract_list_by_reimburse")
    @ApiOperation(value = "报销时获取合同列表")
    public ApiResult<ContractApply> get_contract_list_by_reimburse() {
        return ApiResult.ok(service.get_contract_list_by_reimburse());
    }
}
